//
//  HZWebVc.m
//  微博 OC
//
//  Created by 黑眼圈 on 16/4/21.
//  Copyright © 2016年 heiyan. All rights reserved.
//

#import "HZOAuthVc.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "HZNetTools.h"
#import "HZOAuthModel.h"
#import "HZOAuthVM.h"


@interface HZOAuthVc ()<UIWebViewDelegate>

@end
// APPKEY
static NSString* WB_APP_KEY = @"2817770965";
// 回调页一定要与在开放平台上面设置的一模一样
static NSString* WB_REDIRECT_URI = @"http://www.itcast.cn";
// APP_SECRET
static  NSString*   WB_APP_SECRET = @"023572891f16991d3fdbcac91dd91b19";

@interface HZOAuthVc()

@property(nonatomic,weak)UIWebView*  webView;


//@property(nonatomic,strong)HZOAuthModel*  model;

@end


@implementation HZOAuthVc


-(void)loadView{
    
    self.view = [[UIWebView alloc]init];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    //打开登录页面
    [self openLogin];
    
    //添加子控件
    [self setupUI];
    
}



#pragma mark -
#pragma mark     添加子控件
-(void)setupUI{
    
    UIBarButtonItem* closeBtn = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
    UIBarButtonItem* autoFillBtn = [[UIBarButtonItem alloc]initWithTitle:@"自动填充" style:UIBarButtonItemStylePlain target:self action:@selector(autofill)];
    
    
    self.navigationItem.leftBarButtonItem = closeBtn;
    self.navigationItem.rightBarButtonItem = autoFillBtn;
}



#pragma mark -
#pragma mark     导航栏按钮的监听方法
-(void)close{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)autofill{
    
//    NSLog(@"填充了");
    
    NSString* jsString = @"document.getElementById('userId').value='409377708@qq.com';document.getElementById('passwd').value='sun409377708'";
  
    //添加JavaScript语言
    [self.webView stringByEvaluatingJavaScriptFromString:jsString];
    
    
}


#pragma mark -
#pragma mark     打开微博登录界面
-(void)openLogin{
    
    NSString* urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",WB_APP_KEY,WB_REDIRECT_URI];
    
    NSURL* url = [NSURL URLWithString:urlString];
    
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    
    UIWebView* webView = (UIWebView*)self.view;
    
    self.webView =webView;
    
    //设置代理
    webView.delegate =self;
    
    [webView loadRequest:request];
    
}


#pragma mark -
#pragma mark     实现 webView 的代理方法
-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    [SVProgressHUD show];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    
    [SVProgressHUD dismiss];
}


//每次方法时都会调用此方法
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSURL* url = request.URL;
    
    //如果url 是以这个网站开头的
    if ([url.absoluteString hasPrefix:WB_REDIRECT_URI]) {

        
        //如果 url 的参数里有这个字符串
        if ([url.query containsString:@"code="]) {
            
            //截取字符串
            NSRange range = [url.query rangeOfString:@"code="];
            NSString* code = [url.query substringFromIndex:range.location+range.length];
            
            //获取access_token
            [[HZOAuthVM sharedOAuth] giveToken:code :^(BOOL succ) {
                
                //如果成功登录了
                if (succ) {
                    
                    //登录的面积不需要了,把它销毁了再跳转到欢迎界面
                    [self dismissViewControllerAnimated:NO completion:^{
                        
                        
                        //发生通知给 app 代理,让它切换搭配欢迎界面
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"wecome" object:self];
                        
                    }];
                    
                }else{
                    
                    [SVProgressHUD showErrorWithStatus:@"网络获取失败"];
                }
                
            }];
            
            
            return NO;
        }
    }
    
    
    return YES;
}



















@end
